import { track, trigger } from "./effect"

const get = createGetter()
const set = createSetter()
const readonlyGet = createGetter(true)

function createGetter(isReadOnly = false) {
  return function(target, key) {
    const res = Reflect.get(target, key)
    if (!isReadOnly) {
      track(target, key)
    }
    return res
  }
}

function createSetter() {
  return function(target, key, value) {
    Reflect.set(target, key, value)
    trigger(target, key)
    return true
  }
}

export function reactive(raw) {
  return new Proxy(raw, {
    get,
    set,
  })
}

export function readonly(raw) {
  return new Proxy(raw, {
    get: readonlyGet,
    set(target, key, value) {
      console.warn("readonly", target)
      return true
    }
  })
}

